<?php
namespace app\home\logic;

use app\common\logic\Base;
use think\facade\App;
use think\facade\Request;

class UrlGroup extends  Base {
    
    /**
     * @desc 我的链接
     * @param array $params
     * @param int $userId
     * @return array
     * @throws \Exception
     */
    public function index($params, $userId) {
        $list = App::model('UrlGroup')
            ->where('ug_user_id', '=', $userId)
            ->paginate($params['pageSize'], false, ['query' => request()->param()]);

        $pagination = $list->render();
        if($list) {
            $list = $list->toArray();
            $list = output_format($list['data'], 'ug');
            foreach ($list as $k => $v) {
                $list[$k]['count'] = model('UrlGroupRelation')
                    ->where('ugr_group_id', '=', $v['groupId'])
                    ->count();
            }
        }

        return [
            'list'         => $list,
            'params'       => $params,
            'pagination'   => $pagination
        ];
    }

    /**
     * @desc 编辑
     * @param $groupId
     * @param $userId
     * @return array
     * @throws \think\Exception
     */
    public function edit($groupId,$userId) {
        $where = [
            'ug_group_id' => $groupId,
            'ug_user_id'  => $userId
        ];
        $field = [
            'ug_group_id AS groupId',
            'ug_group_name AS groupName',
            'ug_status AS status',
            'ug_description AS description',
            'ug_modify_time AS modifyTime',
            'ug_create_time AS createTime'
        ];
        $info = model('UrlGroup')
            ->field($field)
            ->where($where)
            ->find();

        return [
            'info' => $info
        ];
    }

    /**
     * @desc 编辑链接
     * @param array $params
     * @param int $userId
     * @return bool
     * @throws \Exception
     */
    public function operateAjax($params, $userId) {
        $data = [
            'ug_user_id'     => $userId,
            'ug_group_name'  => $params['groupName'],
            'ug_status'      => $params['status'],
            'ug_description' => $params['description'],
            'ug_modify_time' => date('Y-m-d H:i:s')
        ];
        $data = array_filter($data, function($arr) {
            if($arr === '' || $arr === null) {
                return false;
            }
            return true;
        });
        
        if($params['action'] == 'add') {
            $data['ug_create_time'] = date('Y-m-d H:i:s');
            $result = model('UrlGroup')->insertGetId($data);
        }else {
            $Where = [
                'ug_user_id' => $userId,
                'ug_group_id'  => $params['id'],
            ];
            $groupInfo = model('urlGroup')->where($Where)->find();
            if(!$groupInfo) {
                $this->errorCode = EC_HOME_URL_GROUP_NOT_EXIST;
                return false;
            }
            $result = model('urlGroup')
                ->where($Where)
                ->update($data);
        }
        
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
}
